import MqttObserver from './mqtt_observer';
/**
 * Observer 类
 * 
 * 该类实现了一个简单的观察者模式的管理器，主要用于维护一组实现了 `MqttObserver` 接口的观察者。
 * MqttManager 通过继承该类来支持添加、移除、广播 MQTT 事件给各个监听者。
 */
class Observer {
    private _observers: MqttObserver[] = [];
    public get observers() {
        return this._observers || [];
    }
    public addObserver(observers: MqttObserver) {
        if (!this._observers.includes(observers)) {
            this._observers.push(observers);
        }
    }

    public removeObserver(observers: MqttObserver) {
        const index = this._observers.findIndex((item: MqttObserver) => item === observers);
        if (index === -1) return;
        this._observers.splice(index, 1);
    }

    public timer(seconds: number, callback: (...args: any[]) => void) {
        return setTimeout(callback, seconds * 1000);
    }

}

export default Observer;